﻿using System;
using System.Linq;
using Missbot.Core.Base;
using Missbot.Core.SyntaxChecker.ParamCheckers;
using Dolany.UtilityTool;
using JetBrains.Annotations;
using Missbot.AIModule.Base;

namespace Missbot.AIModule.Record
{
    [UsedImplicitly]
    public class CartoonSayingMod : IAIModule
    {
        public override string Name => "语录功能";

        [ModuleEnter
            (Command = "语录",
             Description = "录入一条语录",
             DailyLimit = 5,
             SyntaxHint = "[出处] [人物] [内容]",
             ChannelType = ChannelType.Messaging)]
        public bool ProcceedMsg(InboxSession session, string cartoon, string charactor, UnlimitedStr contentParam)
        {
            var content = contentParam.Data;
            var saying = new Saying
            {
                Id = Guid.NewGuid().ToString(),
                Cartoon = cartoon,
                FromUserID = session.User.ID,
                Charactor = charactor,
                Content = content,
                FromGroupID = session.Group.ID
            };
            saying.Insert();

            session.SendBack("语录录入成功！");
            return true;
        }

        private static void SayingRequest(InboxSession session, string keyword = null)
        {
            var ranSaying = GetRanSaying(session.Group.ID, keyword);
            if (string.IsNullOrEmpty(ranSaying))
            {
                session.SendBack("没有任何相关语录！");
                return;
            }

            session.SendBack(ranSaying);
        }

        private static string GetRanSaying(string fromGroupID, string keyword = null)
        {
            var query = SayingDA.Get(fromGroupID);
            if (keyword != null)
            {
                query = query.Where(p => p.Cartoon.Contains(keyword) ||
                                         p.Charactor.Contains(keyword) ||
                                         p.Content.Contains(keyword)).ToList();
            }

            if (query.IsNullOrEmpty())
            {
                return string.Empty;
            }

            var saying = query.RandElement();
            return GetShownSaying(saying);
        }

        private static string GetShownSaying(Saying s)
        {
            var shownSaying = $@"
    {s.Content}
    ——《{s.Cartoon}》 {s.Charactor}
";

            return shownSaying;
        }

        [ModuleEnter(Command = "语录", Description = "返回一条随机语录", DailyLimit = 20, ChannelType = ChannelType.Messaging)]
        public bool Sayings(InboxSession session)
        {
            SayingRequest(session);
            return true;
        }

        [ModuleEnter(Command = "语录", Description = "按关键字检索语录", SyntaxHint = "[关键字]", ChannelType = ChannelType.Messaging)]
        public bool Sayings_Query(InboxSession session, string key)
        {
            SayingRequest(session, key);
            return true;
        }

        [ModuleEnter(Command = "删除语录", Description = "按关键字删除语录", AuthLvl = AuthorityLevel.Owner, SyntaxHint = "[关键字]", ChannelType = ChannelType.Messaging)]
        public bool ClearSayings(InboxSession session, string key)
        {
            var count = SayingDA.Delete(session.Group.ID, key);
            session.SendBack($"共删除{count}条语录");
            return true;
        }
    }
}